local ItemConfig = require("excel.Item").item
local GemConfig = require("excel.Gem").gem
local EquipExcel = require("excel.Equip")
local EquipConfig = EquipExcel.equip
local EquipQulityConf = EquipExcel.equipQulity
local Map = require("core.Map")

COLOR_WHITE =	1	--白色
COLOR_GREEN =	2	--绿色
COLOR_BLUR =	3	--蓝色
COLOR_PURPLE =	4	--紫色
COLOR_ORANGE =	5	--橙色
COLOR_GOLD =	6	--红色

ITEM_ID_JINGYAN = 10001             --经验
ITEM_ID_YINLIANG = 10002            --银两
ITEM_ID_YUANBAO = 10003             --元宝
ITEM_ID_BIND_YUANBAO = 10004        --绑定元宝
ITEM_ID_ZHANGONG = 10005            --战功
ITEM_ID_ZHANGONG1 = 10012           --战功
ITEM_ID_WUSHUANG = 10006            --无双
ITEM_ID_FAMILYEXP = 10008           --军团经验
ITEM_ID_FAMILYCONTRIBUTION = 10009  --军团贡献
ITEM_ID_LABA = 10036               --喇叭
ITEM_ID_ADD_CAP = 10037            --仓库背包开格子道具
ITEM_ID_FLY = 10030                --飞云符
ITEM_ID_STRENGTH_STONE_1 = 11015     --一级强化石
ITEM_ID_XUBAO_ITEM = 10014          -- 寻宝令
ITEM_ID_LOTTERY    = 10038          -- 彩票
ITEM_ID_DAILY_GIFT = 10027          -- 豪礼兑换券
ITEM_ID_GOLD_MONTH_CARD = 10234     -- 黄金月卡
ITEM_ID_DROP_YUANBAO = 10026        -- 1元宝掉落物
ITEM_ID_GOOD_TAO = 12451            -- 好桃子
ITEM_ID_BAD_TAO = 12452             -- 坏桃子
ITEM_ID_GAIMINGKA = 10020           -- 改名卡


EQUIP_SMELT_MAX_VALUE = 1000
MONEY_TYPE = 7								 -- 货币类型
FREE_STORE_GRID = 56           --免费仓库格子数量
FREE_BAG_GRID = 48             --免费背包格子数量

--spobj是指不能放在背包中，但可以作为掉落物，可以作为附件的东西，如血球，铜币等
is_spobj = {}--判断某个item_id是否spobj
spobj = {}--key是yinliang等，value是对应的item_id的集合

CAP_PER_GRID = 999      --最大叠加量
BAG_MAX_CAP = 240				--背包容量最大值
STORE_MAX_CAP = 280				--仓库容量最大值
LIST_PER_CNT = 48				--每次大列表发送的最大个数
COWRY_MAX_CAP = 168     --寻宝仓库最大值
ADD_CAP_NEED_YUANBAO = 20 --增加背包和仓库格子所需元宝数

BASE_ATTR = 1			--装备基础属性
RAND_ATTR = 2			--装备随机属性
VIP_ATTR = 3			--装备VIP属性

-- 背包操作
BAG_NONE = 0				-- 无标识
BAG_SWAP = 1				-- 交换
BAG_DIVIDE = 2				-- 拆分
BAG_EQUIP = 3				-- 装备
BAG_EQUIP_CHANGE = 4		-- 锻造改变
BAG_RESOLVE = 5				-- 道具分解
BAG_PICKUP = 6				-- 拾取

EQUIP_LEN = 13
WEAPON = 1--武器
TOUSHI = 2--头盔
XIANGLIAN = 3--项链
CLOTH = 4--衣服
HUWAN = 5--护腕
KUZI = 6--裤子
SHOUTAO = 7 --手套
SHOE = 8--鞋子
YAODAI = 9 --腰带
RING = 10    --戒指
YUNZHANG = 11 --勋章
SHENGONG = 12 --神弓
PIFENG   = 13 --披风



--枚举
HUMAN_DIE_CAN_NOT_USE_ITEM = 1   -- 人物死亡不能使用物品
GRID_INDEX_INVALID = 2           -- 格子下标非法
GRID_IS_EMPTY = 3                -- 格子为空
CNT_INVALID = 4					 -- 数量非法
ITEM_CAN_NOT_USE = 5			 -- 物品不能使用
NOT_ENOUGH_LV = 6				 -- 等级不够
HP_FULL = 7						 -- 满血
MP_FULL = 8						 -- 满蓝
CAP_NOT_ENOUGH = 9               -- 容量不足
NOT_FIND_COMMAND = 10			 -- 命令不存在
OVER_TIME = 11					 -- 物品过期
CAN_NOT_IMMEDIATE_USE = 12		 -- 不能直接使用

--错误信息
BAG_ADD_CAP_MAX_CNT = 1 -- 背包格子已到最大
BAG_ADD_CAP_YUANBAO_NOT_ENOUGH = 2 --开启背包元宝不足
BAG_ADD_STORE_MAX_CNT = 3 --仓库各自已到最大
BAG_ADD_STORE_YUANBAO_NOT_ENOUGH = 4 --开启仓库格子元宝不足
CAN_NOT_DISCARD = 5             -- 不能销毁

--元宝熔炼
SMELT_MAX_TIMES = 1       --普通人元宝熔炼最大次数
SMELT_HELP_MORE = 20      --多一个人熔炼多加多少绑定元宝
SMELT_MAX_HELP_PEOPLE = 4 --最多几人帮助
SMELT_HELP_REWARD = 10    --帮助他人一次可得多少绑定元宝
SMELT_CAN_HELP_TIMES = 5  --最多可帮助他人几次
SMELT_NEED_YUANBAO = 50   --熔炼第一次需要消耗多少元宝
SMELT_CAN_GET_BIND = 250  --熔炼一次可得到多少绑定元宝
SMELT_CAN_GET_BIND_2 = 300  --熔炼二次可得到多少绑定元宝
SMELT_CAN_GET_BIND_3 = 450  --熔炼三次可得到多少绑定元宝

--奖励状态
REWARD_NOT_CAN_GET = 0          --不可以领取
REWARD_CAN_GET = 1              --可领取
REWARD_ALREADY_GET = 2          --已领取


qualityOut = qualityOut or {}
function init()
	for i = 1,#EquipQulityConf do 
		if not qualityOut[i] then
			qualityOut[i] = {}
			local t = qualityOut[i]
			local len = 0
			for j = 1,#EquipQulityConf do 
				if i ~= j then
					len = len + 1
					t[len] = j
				end
			end
		end
	end
	for id, v in pairs(ItemConfig) do
		if v.command[1] == "spobj" then
			is_spobj[id] = true
			spobj[v.command[2]] = spobj[v.command[2]] or {}
			spobj[v.command[2]][id] = v.command
		end
	end
end

function isItem(id)
	return id and ItemConfig[id]
end

function isGem(id)
	return id and GemConfig[id]
end

function isEquip(id)
	return id and EquipConfig[id]
end

function getCf(id)
	return ItemConfig[id] or EquipConfig[id] or GemConfig[id] 
end

function getValue(id, attr)
	if not id or not attr then
		return
	end
	if ItemConfig[id] then
		return ItemConfig[id][attr]
	end
	if EquipConfig[id] then
		return EquipConfig[id][attr]
	end
	if GemConfig[id] then
		return GemConfig[id][attr]
	end	
end

function isValidId(id)
	return id and (ItemConfig[id] or EquipConfig[id] or GemConfig[id])
end

function canAppend(id)
	return (isItem(id) and getValue(id,"canAppend") >= 1) or (isGem(id) and getValue(id,"canAppend") >= 1)	
end

function getRandItem(items,rateIndex)
	local sum = 0
	for i = 1,#items do 
		sum = sum + items[i][rateIndex]
	end
	
	local rand = math.random(1,sum)
	sum = 0
	for i = 1,#items do 
		sum = sum + items[i][rateIndex]
		if sum >= rand then
			return items[i]		
		end
	end
end

function getQualityOut(quality)
	return qualityOut[quality]
end

function getItemCntByBag(human)
	local line = human.db.bag[0] / 8 + 1
	for _,v in ipairs(BagConfig.bagCap) do 
		if v.line == line then
			return v.itemCnt
		end
	end
	assert()
end

function getItemCntByStore(human)
	local line = human.db.store[0] / 8 + 1
	for _,v in ipairs(BagConfig.storeCap) do 
		if v.line == line then
			return v.itemCnt
		end
	end
	assert()
end

function getPrice(grid)
	local item_id = grid.id
	local price = getValue(item_id, "price")
	return price
--	return grid.equip and math.floor(price * (1 + (grid.data.equip.strengthenLv or 0) / 10 + (grid.equip.quality - 1) * 3 / 10)) or price
end

function getCoolTimeByGroupID(groupID)
	for _,v in pairs(ItemConfig) do 
		if v.coolGroup == groupID then
			return v.coolTime
		end
	end
end

function getColorByGrid(grid)
	local color = 0
	if isEquip(grid.id) then
		color = grid.data.equip.quality
	else 
		color = getValue(grid.id,"color")	
	end
	return color
end

function getIconByGrid(grid)
	if is_spobj[grid.id] then
		return 0
	else
		return getValue(grid.id,"icon")
	end
end

function getRealQuality(quality)
  if quality <= 1 then
    return 1
  elseif quality <= 3 then
    return 2
  elseif quality <= 6 then
    return 3
  elseif quality <= 10 then
    return 4
  elseif quality <= 15 then
    return 5
  elseif quality <= 16 then
    return 6
  end
  return 1
end

function getText(quality)
  if quality == 1 then
    return "白色"
  elseif quality == 2 then
    return "绿色"
  elseif quality == 3 then
    return "绿色+1"
  elseif quality == 4 then
    return "蓝色"
  elseif quality == 5 then
    return "蓝色+1"
  elseif quality == 6 then
    return "蓝色+2"
  elseif quality == 7 then
    return "紫色"
  elseif quality == 8 then
    return "紫色+1"
  elseif quality == 9 then
    return "紫色+2"
  elseif quality == 10 then
    return "紫色+3"
  elseif quality == 11 then
    return "橙色"
  elseif quality == 12 then
    return "橙色+1"
  elseif quality == 13 then
    return "橙色+2"
  elseif quality == 14 then
    return "橙色+3"
  elseif quality == 15 then
    return "橙色+4"
  elseif quality == 16 then
    return "红色"      
  end
end

init()
